package com.itheima.pinda.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.pinda.DTO.angency.FleetDto;
import com.itheima.pinda.common.utils.Constant;
import com.itheima.pinda.common.utils.PageResponse;
import com.itheima.pinda.entity.agency.PdFleet;
import com.itheima.pinda.mapper.PdFleetMapper;
import com.itheima.pinda.mapstruct.TruckDTO2Entity;
import com.itheima.pinda.service.PdFleetService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class PdFleetServiceImpl extends ServiceImpl<PdFleetMapper, PdFleet> implements PdFleetService {

    @Resource
    private TruckDTO2Entity truckDTO2Entity;


    /**
     * 添加车队
     *
     * @param dto 车队信息
     * @return 车队信息
     */
    @Override
    public FleetDto saveAgencyType(FleetDto dto) {
        PdFleet pdFleet = truckDTO2Entity.conver(dto);
        super.save(pdFleet);
        dto = truckDTO2Entity.conver(pdFleet);
        return dto;
    }

    /**
     * 获取车队分页数据
     *
     * @param page        页码
     * @param pageSize    页尺寸
     * @param name        车队名称
     * @param fleetNumber 车队编号
     * @param manager     负责人id
     * @return 车队分页数据
     */
    @Override
    public PageResponse<FleetDto> findByPage(Integer page, Integer pageSize,
                                             String name, String fleetNumber, String manager) {
        Page<PdFleet> pdFleetPage = super.page(new Page<>(page, pageSize), Wrappers.<PdFleet>lambdaQuery()
                .like(StringUtils.isNotBlank(name), PdFleet::getName, name)
                .eq(StringUtils.isNotBlank(fleetNumber), PdFleet::getFleetNumber, fleetNumber)
                .eq(StringUtils.isNotBlank(manager), PdFleet::getManager, manager)
                .eq(PdFleet::getStatus, Constant.DATA_DEFAULT_STATUS)
                .orderByAsc(PdFleet::getId));

        return PageResponse.<FleetDto>builder()
                .page(page)
                .pagesize(pageSize)
                .pages(pdFleetPage.getPages())
                .counts(pdFleetPage.getTotal())
                .items(pdFleetPage.getRecords()
                        .stream()
                        .map(item -> truckDTO2Entity.conver(item))
                        .collect(Collectors.toList()))
                .build();
    }


    /**
     * 获取车队列表
     *
     * @param ids      车队Id列表
     * @param agencyId 机构id
     * @return 车队列表
     */
    @Override
    public List<FleetDto> findAll(List<String> ids, String agencyId) {
        List<PdFleet> pdFleetList = super.list(Wrappers.<PdFleet>lambdaQuery()
                .in(ids != null && ids.size() > 0, PdFleet::getId, ids)
                .eq(StringUtils.isNotBlank(agencyId), PdFleet::getAgencyId, agencyId)
                .eq(PdFleet::getStatus, Constant.DATA_DEFAULT_STATUS)
                .orderByAsc(PdFleet::getId));

        return pdFleetList.stream()
                .map(item -> truckDTO2Entity.conver(item))
                .collect(Collectors.toList());
    }


    /**
     * 更新车队信息
     *
     * @param id  车队id
     * @param dto 车队信息
     * @return 车队信息
     */
    @Override
    public FleetDto updateFleet(String id, FleetDto dto) {
        dto.setId(id);
        PdFleet pdFleet = truckDTO2Entity.conver(dto);
        super.updateById(pdFleet);
        return dto;
    }


}
